package com.heima.article.listener;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.article.service.ApArticleConfigService;
import com.heima.common.constants.message.WmNewsMessageConstants;
import com.heima.model.article.pojos.ApArticleConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * 监听wemedia上下架文章操作
 */

@Component
@Slf4j
public class ArticleIsDownListener {

    @Autowired
    ApArticleConfigService apArticleConfigService;

    /**
     * 接收消息方法
     * 指定kafka监听的主题名称
     * @param mapJson
     */
    @KafkaListener(topics = WmNewsMessageConstants.WM_NEWS_UP_OR_DOWN_TOPIC)
    public void recMessage(String mapJson){
        log.info("============>接收到文章上下架的消息");
        //消息反序列化至map
        Map map = JSON.parseObject(mapJson, Map.class);
        Long articleId = (Long)map.get("articleId");
        Object enable = map.get("enable");
        // 判断:如果enable=1,此时状态为上架 则 isDown状态为false, 反之为true
        boolean isDown = enable.equals(1) ? false : true;
        //根据接收到的消息(文章id),修改文章config表中的信息
        apArticleConfigService.update(Wrappers.<ApArticleConfig>lambdaUpdate()
                .set(ApArticleConfig::getIsDown,isDown)
                .eq(ApArticleConfig::getArticleId,articleId)
        );
    }

}
